	.text
	.align	2
start:
	MOV	r0, #array
	MOV	r1, #0
	MOV	r2, #7
	LDW sp, [r0+], #32
	B.L	quicksort
	HLT
quicksort:
	SUB sp, sp, #16
	STW lr, [sp+], #12
	STW sl, [sp+], #8
	STW r24, [sp+], #4
	STW r23, [sp+], #0
	MOV	r23, r0
	MOV	r14, r1
	MOV	r24, r2
	CMPSUB.A	r2, r1
	BEL	L5
	MOV	r12, r0
	LDW	r15, [r12+], r2 << #2
	ADD r12, r12, r2 << #2
	ADD	r13, r0, r1 << #2
	MOV	r11, r1
	MOV	sl, r1
L4:
	LDW	r10, [r13+], #0
	CMPSUB.A	r15, r10
	BSL	L3
	MOV	r8, r23
	LDW	r9, [r8+], sl << #2
	ADD r8, r8, sl << #2
	STW	r10, [r8+], #0
	STW	r9, [r13+], #0
	ADD	sl, sl, #1
L3:
	ADD	r11, r11, #1
	ADD	r13, r13, #4
	CMPSUB.A	r24, r11
	BSG	L4
	MOV	r13, r23
	LDW	r11, [r13+], sl << #2
	ADD r13, r13, sl << #2
	STW	r11, [r12+], #0
	STW	r15, [r13+], #0
	MOV	r0, r23
	MOV	r1, r14
	SUB	r2, sl, #1
	B.L	quicksort
	MOV	r0, r23
	ADD	r1, sl, #1
	MOV	r2, r24
	B.L	quicksort
L5:
	LDW r23, [sp+], #0
	LDW r24, [sp+], #4
	LDW sl, [sp+], #8
	LDW lr, [sp+], #12
	ADD sp, sp, #16
	JUMP lr

array:
	.long  5
	.long  1
	.long  6
	.long  4
	.long  7
	.long  8
	.long  2
	.long  3
	.long  1000
